package nioTest.bio;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIOServer {

    public static final Logger logger = LogManager.getLogger(BIOServer.class);

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        try {
            ServerSocket serverSocket = new ServerSocket(6666);

            while (true) {
                // 此处会阻塞
                Socket accept = serverSocket.accept();
                executorService.execute(()->{
                    try {
                        InputStream inputStream = accept.getInputStream();
                        byte[] bytes = new byte[1024];
                        while (true){
                            // 此处会阻塞
                            int read = inputStream.read(bytes);
                            System.out.println(new String(bytes, 0, read));
                            if(read == -1){
                                break;
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
